﻿using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Castle.Facilities.Logging;
using Abp.AspNetCore;
using Abp.Castle.Logging.Log4Net;
using KelonProud.Authentication.JwtBearer;
using KelonProud.Configuration;
using KelonProud.Identity;
using KelonProud.Web.Resources;
using Abp.AspNetCore.SignalR.Hubs;
using Abp.Hangfire;
using Hangfire;
using Hangfire.MySql.Core;
using KelonProud.Administration.Managers.Identity;
using KelonProud.EntityFrameworkCore;
using KelonProud.Mvc;
using KelonProud.Mvc.ViewEngine;


namespace KelonProud.Web.Startup
{
    public class Startup
    {
        private readonly IConfigurationRoot _appConfiguration;

        public Startup(IHostingEnvironment env)
        {
            _appConfiguration = env.GetAppConfiguration();
        }

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            //services.AddHangfire(config =>
            //{
            //    config.UseStorage(new MySqlStorage(_appConfiguration.GetConnectionString("Default")));
            //});

            // MVC
            services.AddMvc(
                options =>
                {
                    options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
                    options.Filters.AddService(typeof(UnhandledExceptionFilter), 20);
                }).AddRazorOptions(options =>
                {
                    options.FileProviders.Add(new KelonProudViewVirtualPathProvider());
                });


            IdentityRegistrar.Register(services);
            AuthConfigurer.Configure(services, _appConfiguration);

            services.AddScoped<IWebResourceManager, WebResourceManager>();

            services.AddSignalR();
            services.AddSession();

            services.AddSession(options =>
            {
                options.IdleTimeout = TimeSpan.FromMinutes(300);
            });

            // Configure Abp and Dependency Injection
            return services.AddAbp<KelonProudWebMvcModule>(
                // Configure Log4Net logging
                options => options.IocManager.IocContainer.AddFacility<LoggingFacility>(
                    f => f.UseAbpLog4Net().WithConfig("log4net.config")
                )
            );
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            //var jobOptions = new BackgroundJobServerOptions
            //{
            //    ServerName = "CssServer"
            //};
            //app.UseHangfireServer(jobOptions);
            //app.UseHangfireDashboard();

            app.UseAbp(); // Initializes ABP framework.
            app.UseSession();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            app.UseStaticFiles();

            app.UseAuthentication();

            app.UseJwtTokenMiddleware();

            //app.UseSignalR(routes =>
            //{
            //    routes.MapHub<AbpCommonHub>("/signalr");
            //});

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "defaultWithArea",
                    template: "{area}/{controller=Home}/{action=Index}/{id?}");

                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
